/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.edu.agh.student.logic.webapp;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Identity;
import org.jbpm.api.HistoryService;
import org.jbpm.api.history.HistoryProcessInstance;
import pl.edu.agh.student.logic.service.ApplicationServiceImpl;
import pl.edu.agh.student.logic.service.UserServiceImpl;
import pl.edu.agh.student.logic.util.ServiceProvider;
import pl.edu.agh.student.model.Application;
import pl.edu.agh.student.model.Attachment;

/**
 *
 * @author janburkot
 */
@Stateful
@Name("processBean")
@Scope(ScopeType.CONVERSATION)
public class ProcessBeanImpl  implements ProcessBean, Serializable {

    @In("serviceProvider")
    protected ServiceProvider serviceProvider;
    @In(value="userService",create=true,required=false)
    @Out
    protected UserServiceImpl userService;
    @In
    Identity identity;
    @Logger
    protected Log log;
    
    @In(value="applicationService",create=true,required=false)
    @Out
    protected ApplicationServiceImpl applicationService;
    
    
    private String processId;
    
    @Destroy
    @Remove
    public void destroy() {
        log.debug("destry component #0", this);
    }
    
    public List<HistoryProcessInstance> getAll() {
        HistoryService historyService = serviceProvider.getHistoryService();
        System.out.println(historyService);
        System.out.println(historyService.createHistoryActivityInstanceQuery().list());
        System.out.println(historyService.createHistoryProcessInstanceQuery().list());
        System.out.println(historyService.createHistoryTaskQuery().list());
        return historyService.createHistoryProcessInstanceQuery().list();
    }

    public Application getApplication(HistoryProcessInstance process) {
        HistoryService historyService = serviceProvider.getHistoryService();
        System.out.println("==============================================");
        System.out.println(historyService);
        System.out.println(process);
        System.out.println(process.getProcessInstanceId());
        System.out.println(historyService.getVariable(process.getProcessInstanceId(), "applicationId"));
        Long applicationId = new Long((String)historyService.getVariable(process.getProcessInstanceId(), "applicationId"));
        System.out.println(applicationId);
        System.out.println(historyService.getVariableNames(process.getProcessInstanceId()));
        System.out.println("==============================================");
        
        return applicationService.find(applicationId);
        
    }

    public HistoryProcessInstance getCurrentProcess() {
        HistoryService historyService = serviceProvider.getHistoryService();
        System.out.println(historyService);
        HistoryProcessInstance process = historyService.createHistoryProcessInstanceQuery().processInstanceId(processId).uniqueResult();
        System.out.println(processId);
        System.out.println(process);
        return process; 
    }

    public void setSelectedProcess(HistoryProcessInstance process) {
        System.out.println("setSelectedTask");
        System.out.println(process);
        this.processId = process.getProcessInstanceId();
    }

    public List<Attachment> getFiles() {
        List<Attachment> files = new LinkedList<Attachment>();
        HistoryProcessInstance process = getCurrentProcess();
        Application application = getApplication(process);
        files.addAll(application.getAttachments());
        return files;
    }
}
